/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ASTEC.Bean;

import ASTEC.DaoImpl.UsuarioDAOImpl;
import ASTEC.Dao.UsuarioDAO;
import ASTEC.Dominio.Usuario;
import java.awt.event.ActionEvent;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;

/**
 *
 * @author KeilyI
 */
@ManagedBean
@SessionScoped
public class LoginBean {
    
    UsuarioDAO usuarioDAO=new UsuarioDAOImpl();
    private String username;  
    private String password;  
    public String getUsername() {  
        return username;  
    }  
  
    public void setUsername(String username) {  
        this.username = username;  
    }  
  
    public String getPassword() {  
        return password;  
    }  
  
    public void setPassword(String password) {  
        this.password = password;  
    }  
  
    public String login() {            
        Usuario usuario=usuarioDAO.obtenerUsuario(username, password);
        String vista;
        FacesMessage msg;
        if(usuario!=null) {  
            msg = new FacesMessage("Bienvenido", username);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            int idRol=usuario.getRol().getIdRol();
          
            if(idRol == 104)
                return "Enviar_solicitud";
            else if(idRol == 100)
                return "opciones"; 
            else if(idRol == 101)
                return "opcionesTecnico";
            else{
                msg = new FacesMessage("Rol no creado =P", "");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return "index";
            }
        } else {  
            msg = new FacesMessage("Usuario y/o Contraseña incorrectos", "");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return "index";
        }  
          
        
    }  
    
}
